package sonoCommunity.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import sonoCommunity.domain.User;

@Repository
@Transactional
public class UserDao {

	@PersistenceContext
	EntityManager em;

	public void add(User user) {
		em.persist(user);
	}

	public void update(User user) {
		em.merge(user);
	}

	public void deleteUsers(List<User> userList) {
		em.createQuery("delete from User u where u in (:userList)")
				.setParameter("userList", userList).executeUpdate();
	}

	public List<User> getUserBylastName(String name) {
		return em.createQuery("Select u from User u where u.lastName = :firstName")
				.setParameter("firstName", name).getResultList();

	}
	//use to check if login/password exist
	public String getUserPassword(String userMail){
		User u = new User();
		u= (User)em.createQuery("Select u from User u where u.mail =:userMail")
				.setParameter("userMail", userMail).getSingleResult();
		return u.getPassword();
	}

}
